package xj.graph2d.app.prop;

import static xj.graph2d.app.resources.Resources.getResourceString;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

import xj.graph2d.SimpleDrawView;
import xj.graph2d.View;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;
import xj.util.gui.ResourceLoader;

public class OptionListener 
  extends DrawAction 
  implements ActionListener {

  protected static String[] bgnames = { "none", "kenwimer" };

  protected static Image[] bgimages = {
      null,
      ResourceLoader.loadImage("tiles/kenwimer")
  };

  class BgImageOptionPanel extends DialogPanel {

    public BgImageOptionPanel(String name) {
      super(name);

      JLabel imageLabel = new JLabel(getResourceString("BackgroundImage.text") + " ");
      imageChoice = new JComboBox();
      for (int i = 0; i < bgnames.length; i++) {
	imageChoice.addItem(bgnames[i]);
      }
      addRow(imageLabel, imageChoice);

      setOption();
    }

    @Override
    public void applyOption() {
      SimpleDrawView view = main.getView();
      view.setBackgroundImage(getSelectedImage());
      view.refresh();
    }

    public Image getSelectedImage() {
      int i = imageChoice.getSelectedIndex();
      if (i >= 0 && i < bgimages.length) {
	return bgimages[i];
      }
      return null;
    }

    protected JComboBox imageChoice;

  }

  class GridOptionPanel extends DialogPanel {

    public GridOptionPanel(String name) {
      super(name);

      JLabel onOffLabel = new JLabel(getResourceString("GridOnOff.text"));
      onOffLabel.setVerticalAlignment(SwingConstants.TOP);
      JPanel onOffPanel = new JPanel();
      onOffPanel.setLayout(new BoxLayout(onOffPanel, BoxLayout.X_AXIS));
      gridOn = new JRadioButton(getResourceString("On.text"));
      gridOff = new JRadioButton(getResourceString("Off.text"));
      ButtonGroup group1 = new ButtonGroup();
      group1.add(gridOn);
      group1.add(gridOff);
      onOffPanel.add(gridOn);
      onOffPanel.add(gridOff);

      JLabel sizeLabel = new JLabel(getResourceString("GridSize.text"));
      sizeLabel.setVerticalAlignment(SwingConstants.TOP);
      JPanel sizePanel = new JPanel();
      sizePanel.setLayout(new BoxLayout(sizePanel, BoxLayout.X_AXIS));
      gridSmall = new JRadioButton(getResourceString("Small.text"));
      gridLarge = new JRadioButton(getResourceString("Large.text"));
      ButtonGroup group2 = new ButtonGroup();
      group2.add(gridSmall);
      group2.add(gridLarge);
      sizePanel.add(gridSmall);
      sizePanel.add(gridLarge);

      JLabel typeLabel = new JLabel(getResourceString("GridLine.text"));
      typeLabel.setVerticalAlignment(SwingConstants.TOP);
      JPanel typePanel = new JPanel();
      typePanel.setLayout(new BoxLayout(typePanel, BoxLayout.Y_AXIS));
      // gridDot = new JRadioButton("Dot");
      gridLine = new JRadioButton(getResourceString("SolidLine.text"));
      gridDash = new JRadioButton(getResourceString("DashedLine.text"));
      ButtonGroup group3 = new ButtonGroup();
      group3.add(gridLine);
      group3.add(gridDash);
      // group3.add(gridDot);
      typePanel.add(gridLine);
      typePanel.add(gridDash);
      // typePanel.add(gridDot);

      addRow(onOffLabel, onOffPanel);
      addRow(sizeLabel, sizePanel);
      addRow(typeLabel, typePanel);

      setPreferredSize(new Dimension(250, 150));
      setOption();
    }

    @Override
    public void setOption() {
      SimpleDrawView view = main.getView();
      boolean on = view.isGridOn();
      gridOn.setSelected(on);
      gridOff.setSelected(!on);
      int size = view.getGridSize();
      gridSmall.setSelected(size == View.GRID_SMALL);
      gridLarge.setSelected(size == View.GRID_LARGE);
      int type = view.getGridType();
      // gridDot.setSelected(type == View.GRID_DOT);
      gridLine.setSelected(type == View.GRID_SOLID_LINE);
      gridDash.setSelected(type == View.GRID_DASHED_LINE);
    }

    @Override
    public void applyOption() {
      SimpleDrawView view = main.getView();
      if (gridOn.isSelected()) {
	view.setGridOn(true);
	if (gridSmall.isSelected()) {
	  view.setGridSize(View.GRID_SMALL);
	} else if (gridLarge.isSelected()) {
	  view.setGridSize(View.GRID_LARGE);
	} else {
	  view.setGridSize(View.GRID_CUSTOM);
	}
	// if (gridDot.isSelected()) {
	// view.setGridType(View.GRID_DOT);
	// } else
	if (gridLine.isSelected()) {
	  view.setGridType(View.GRID_SOLID_LINE);
	} else if (gridDash.isSelected()) {
	  view.setGridType(View.GRID_DASHED_LINE);
	} else {
	  view.setGridType(View.GRID_DOT);
	}
      } else {
	view.setGridOn(false);
      }
      view.refresh();
    }

    protected JRadioButton gridOn;

    protected JRadioButton gridOff;

    protected JRadioButton gridSmall;

    protected JRadioButton gridLarge;

    protected JRadioButton gridDot;

    protected JRadioButton gridLine;

    protected JRadioButton gridDash;

  }

  protected TitledDialogPanel gridOptionDialog = null;
  protected GridOptionPanel gridOptionPanel = null;
  protected boolean showTitlePane = false;

  public void actionPerformed(ActionEvent e) {
    if (gridOptionDialog == null) {
      //String gridPanelTitle = getResourceString("Grid.title");
      String gridDialogTitle = getResourceString("BackgroundDialog.title");
      String gridHeader = getResourceString("GridDialog.header");
      String[] gridText = new String[] { 
	getResourceString("GridDialog.text1"),
	getResourceString("GridDialog.text2"),	
      };
      DialogPanel[] options = { 
	gridOptionPanel = new GridOptionPanel(null), 
	//getResourceString("Grid.title")),
	// new BgImageOptionPanel("Background image"),
      };
      Icon icon = DrawApp.getIcon("PrefWizIcon");

      gridOptionDialog = new TitledDialogPanel(main.getApplicationFrame().getJFrame(), 
					       JOptionPane.OK_CANCEL_OPTION, 
					       null, options);
      gridOptionDialog.setTitle(gridDialogTitle);
      if (showTitlePane) { 
	gridOptionDialog.setTitlePaneText(gridHeader, gridText, icon);
      }
    }
    gridOptionDialog.setOptions();
    gridOptionDialog.showOptionDialog();
  }

}
